public class ONEN_TEST_Opp_GivingHistoryRollup {

// Written by Matthew Scholtz, copyright (c) 2007 ONE/Northwest
// This program is released under the GNU General Public License. http://www.gnu.org/licenses/

	private static string GiftRectype = ONEN_Constants.OPP_DEFAULT_RECTYPE_FORTESTS;

	// TBD test undelete
	
	// TBD test changing close date to different year
	
	// TBD test non-gift opp rectype
	
	// TBD test account rollup
	// TBD test changing from one acct to another?


	static testMethod void OneContactMultipleOpps() {

		integer howMany = 1;
		Date datToday = System.Today();
		Date dat1YearAgo = Date.newInstance( datToday.year()-1,1,1);
		Date dat2YearAgo = Date.newInstance( datToday.year()-2,1,1);
		Date dat4YearAgo = Date.newInstance( datToday.year()-4,1,1);
			
		// create & insert contact(s)
		Contact[] TestCons = ONEN_TEST_UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		insert TestCons;
		
		system.debug ( 'TEST>>>>> inserting gift 1...');
		
		// create a new gift for this yr
		Opportunity[] testGift1 = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datToday, 100 , GiftRectype);
		insert testGift1 ;
		
		// reset the conrole creation flag so we'll get conroles for this 2nd insert
		ONEN_OppContactRoles.haveCheckedContactRoles = false;

		system.debug ( 'TEST>>>>> inserting gift 2...');
		
		//create a 2nd gift for last yr
		Opportunity[] testGift2 = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', dat1YearAgo, 60, GiftRectype);
		ONEN_OppContactRoles.haveCheckedContactRoles = false;
		insert testGift2;
		
		//now test that the contact has received the proper stats from the trigger
		id ThisConId = TestCons[0].id;
		Contact UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 160 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 100 , UpdatedCon.Total_Gifts_YTD__c );
		System.AssertEquals ( 60 , UpdatedCon.Total_Gifts_Last_CY__c );


		system.debug ( 'TEST>>>>> changing gift 1...');
		
		// now chg the amts for both opps (cheapskate!)
		testGift1[0].Amount = 50;
		update TestGift1;

		system.debug ( 'TEST>>>>> changing gift 2...');
		
		testGift2[0].Amount=25;
		update TestGift2;
		
		//now test that the contact has updated stats from the trigger
		ThisConId = TestCons[0].id;
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 50 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		

		system.debug ( 'TEST>>>>> inserting gift 3...');

		// now create a gift from 2 yrs ago
		Opportunity[] testGift3 = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', dat2YearAgo, 10 , GiftRectype);
		ONEN_OppContactRoles.haveCheckedContactRoles = false;
		insert testGift3;

		//now test that the contact has updated stats from the trigger
		ThisConId = TestCons[0].id;
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 85 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 50 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );

		// add another from this year (to test adding)
		system.debug ( 'TEST>>>>> inserting gift 4...');
		Opportunity[] testGift4 = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datToday, 25 , GiftRectype);
		ONEN_OppContactRoles.haveCheckedContactRoles = false;
		insert testGift4;

		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 110 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );


		// TBD add a gift from longer ago
		system.debug ( 'TEST>>>>> inserting gift 5...');
		Opportunity[] testGift5 = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', dat4YearAgo, 200 , GiftRectype);
		ONEN_OppContactRoles.haveCheckedContactRoles = false;
		insert testGift5;
		
		// totals should not have changed, except lifetime
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 310 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
		
		// TBD add non-won gift
		system.debug ( 'TEST>>>>> inserting gift 6...');
		Opportunity[] testGift6 = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Prospecting', dat4YearAgo, 35 , GiftRectype);
		ONEN_OppContactRoles.haveCheckedContactRoles = false;
		insert testGift6;
		
		// totals should not have changed at all
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 310 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 75 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
		
		// now delete the 1st gift (now at $50), totals should decrease
		system.debug ( 'TEST>>>>> deleting gift 1...');	
		delete testGift1;
		
		UpdatedCon = [SELECT Id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :ThisConId];
		
		System.AssertEquals ( 260 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 25 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 10 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
	}


	static testMethod void testGivingRollupBulk () {
	
		// for a single contact w/ no previous mbrships, add a new membership
		// and test mbr stats are created
	
		integer howMany = 100;
		Date datClose = System.Today();
			
		// create & insert contact(s)
		Contact[] TestCons = ONEN_TEST_UnitTestData.CreateMultipleTestContacts ( howMany ) ;
		insert TestCons;
		
//		system.debug (TestCons);
		
		// create new opps
		Opportunity[] newOpps = ONEN_TEST_UnitTestData.OppsForContactList ( TestCons, null, 'Closed Won', datClose, 100 , GiftRectype);

		// insert the opp(s)
		Test.StartTest();
		insert newOpps;
		Test.StopTest();
		
		//now test that a contact has received the proper member stats from the trigger
		id FirstConId = TestCons[10].id;
		Contact UpdatedCon = [SELECT id, Total_Gifts_YTD__c, Total_Gifts_Lifetime__c, Total_Gifts_Last_CY__c, Total_Gifts_2_CY_Ago__c FROM Contact WHERE Id = :FirstConId];
		
		System.AssertEquals ( 100 , UpdatedCon.Total_Gifts_Lifetime__c );
		System.AssertEquals ( 100 , UpdatedCon.Total_Gifts_YTD__c );		
		System.AssertEquals ( 0 , UpdatedCon.Total_Gifts_Last_CY__c );
		System.AssertEquals ( 0 , UpdatedCon.Total_Gifts_2_CY_Ago__c );
		
	}


	static testMethod void testNoCR () {
	
		// make sure we can handle an opp w/ missing CR
	
		Date datClose = System.Today();
			
		// create & insert contact(s)
		Contact TestCon = ONEN_TEST_UnitTestData.CreateTestContact ( 1 ) ;
		insert TestCon;
		
		// create a new membership opp without a CR
		
		// look up the id for the specified rectype
		ONEN_RecordTypes rtlookup = new ONEN_RecordTypes ('Opportunity');
		id rtid = rtlookup.GetRecordTypeId (GiftRectype);

		Opportunity newOpp = new Opportunity (
			Name = TestCon.FirstName + ' ' + TestCon.LastName + ' ' + 'Gift 2007',
			Amount = 50,
			StageName = 'Closed Won',
			CR_Contact_Id__c = '701700000005dyz',  //this was taken from a campaign rec, so should never be a contact
			CloseDate = datClose,
			RecordTypeId = rtid
		);

		// nothing to assert, just shouldn't throw an error
		insert newOpp;
		
	}	

}